package net.wenku8.reader.view.common

import android.net.Uri
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import net.wenku8.reader.R
import net.wenku8.reader.enums.ColorType
import net.wenku8.reader.ui.lite.LiteLine
import net.wenku8.reader.ui.lite.LiteText
import net.wenku8.reader.ui.lite.LiteTextButton
import net.wenku8.reader.ui.lite.mediumPadding
import net.wenku8.reader.ui.lite.onClick

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ReadBottom(){
    val readSettingModel = viewModel<ReadSettingModel>()
    readSettingModel.initData()
    if (readSettingModel.show){
        val sheetState = rememberModalBottomSheetState()
        val launcher = rememberLauncherForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
            readSettingModel.selectFile(uri)
        }
        ModalBottomSheet(
            onDismissRequest = {
                readSettingModel.show = false
            },
            sheetState = sheetState
        ) {
            // Sheet content
            Column(
                modifier = Modifier.padding(bottom = 30.dp)
            ) {
                LiteLine {
                    LiteText(text = "字体")
                    LiteTextButton("A-") { readSettingModel.changeFontSize(false) }
                    LiteText(text = "" + readSettingModel.fontValue)
                    LiteTextButton("A+") { readSettingModel.changeFontSize(true) }
                }
                LiteLine {
                    LiteText(text = "行距")
                    LiteTextButton("-") { readSettingModel.changeSpaceSize(false) }
                    LiteText(text = "" + readSettingModel.spaceValue)
                    LiteTextButton("+") { readSettingModel.changeSpaceSize(true) }
                }
                LiteLine {
                    LiteText(text = "背景")
                    ColorType.entries.forEach { colorType ->
                        Box(
                            modifier = Modifier
                                .mediumPadding()
                                .size(44.dp)
                                .clip(shape = CircleShape)
                                .background(colorType.bgColor)
                                .border(width = 1.dp, color = Color.Black, shape = CircleShape)
                                .onClick {
                                    if (colorType.type != ColorType.TYPE99.type) {
                                        readSettingModel.changeBgColor(colorType)
                                    } else {
                                        launcher.launch("image/*")
                                    }
                                }
                        ){
                            when(colorType.type){
                                ColorType.TYPE10.type -> {
                                    Image(
                                        painter = painterResource(id = R.mipmap.reader_bg_yellow),
                                        contentDescription = null
                                    )
                                }
                                ColorType.TYPE99.type -> {
                                    Icon(
                                        modifier = Modifier.size(44.dp),
                                        imageVector = Icons.Outlined.Add,
                                        contentDescription = null
                                    )
                                }
                                else -> {

                                }
                            }
                        }
                    }
                }
                LiteLine{
                    LiteText(text = "左右滑动可以切换章节")
                }
            }
        }
    }

}